<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.interfaces.suntai.utils.CalUtil" %>
<%@ page import="weaver.interfaces.suntai.utils.InvokeHelper" %>
<%@ page import="weaver.interfaces.suntai.utils.SunTaiUtil" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="baseBean" class="weaver.general.BaseBean"/>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="baseBean" class="weaver.general.BaseBean"/>
<%
    JSONObject returnJson = new JSONObject();
    User user = HrmUserVarify.getUser( request, response );
    int userid = user.getUID();
    int billid = 288;
    String tablename = "uf_xshttz";
    String ids = Util.null2String( request.getParameter( "ids" ) );
    rs.executeQuery( "select * from " + tablename + " where id in (" + ids + ")" );
    int counts = rs.getCounts();
    int failureCount = 0;
    int successCount = 0;
    while (rs.next()) {
        JSONObject jsonData = new JSONObject();
        String mainid = Util.null2String( rs.getString( "id" ) );
        jsonData.put( "companyCode", "01-0070-202202-001" );//企业编号
        jsonData.put( "contractCode", Util.null2String( rs.getString( "htbh1" ) ) );//合同编号
        jsonData.put( "contractName", Util.null2String( rs.getString( "htmc" ) ) );//合同名称
        jsonData.put( "contractSecretLev", Util.null2String( rs.getString( "htmj" )));
        String cgf = Util.null2String( rs.getString( "cgf" ) );
        jsonData.put( "purchaserName", SunTaiUtil.getSelectItem( billid, "cgf", cgf ) );//采购方
        jsonData.put( "purchaserCode", Util.null2String( rs.getString( "cgrdm" ) ) );//采购方统一社会信用代码
        jsonData.put( "contKeepStatus", Util.null2String( rs.getString( "lyztwb" ) ) );//合同履约状态
        jsonData.put( "purchaseUse", Util.null2String( rs.getString( "cgrdm" ) ) );//采购方统一社会信用代码
        jsonData.put( "capitalSource", Util.null2String( rs.getString( "zjlywb" ) ) );//资金来源
        jsonData.put( "pricingManner", Util.null2String( rs.getString( "jjfswb" ) ) );//计价方式
        jsonData.put( "technicalWeight", Util.null2String( rs.getString( "jsqc" ) ) );//技术权重
        jsonData.put( "businessWeight", Util.null2String( rs.getString( "swqc" ) ) );//商务权重
        jsonData.put( "payWay", Util.null2String( rs.getString( "fkfswb" ) ) );//付款方式
        String cgfs = Util.null2String( rs.getString( "cgfs" ) );
        jsonData.put( "purchaseWay", Util.null2String( rs.getString( "cgfswb" ) ) );//采购方式
        jsonData.put( "purchaseOrgForm", Util.null2String( rs.getString( "cgzzxswb" ) ) );//采购组织形式
        jsonData.put( "procurementPlatform", Util.null2String( rs.getString( "dzzbpt" ) ) );//电子招标平台
        jsonData.put( "contractAmount", Util.null2String( rs.getString( "htje" ) ) );//合同金额
        jsonData.put( "currency", Util.null2String( rs.getString( "bzwb" ) ) );//币种
        jsonData.put( "exchangeRate", Util.null2String( rs.getString( "hl" )));//汇率
        String se = Util.null2String( rs.getString( "se" ) );
        String sevalue =SunTaiUtil.getSelectItem( billid, "se", se );
        sevalue = CalUtil.fDivide2(sevalue.replace( "%",""),"100",4);
        jsonData.put( "taxRate", sevalue);//税率
        jsonData.put( "signTime", Util.null2String( rs.getString( "qdsj" ) ) );//签订时间
        jsonData.put( "effectiveDate", Util.null2String( rs.getString( "sxsj" ) ) );//生效时间
        jsonData.put( "startTime", Util.null2String( rs.getString( "htkssj" ) ) );//开始时间
        jsonData.put( "endTime", Util.null2String( rs.getString( "htjssj" ) ) );//结束时间
        jsonData.put( "fulfillmentPlace", Util.null2String( rs.getString( "lyddwb" ) ) );//履约地点
        jsonData.put( "supplierEvaluation", Util.null2String( rs.getString( "lypj" ) ) );//履约评价
        jsonData.put( "tender", "1".equals( cgfs ) ? "Y" : "N" );//履约评价
        jsonData.put( "blockNumber", Util.null2String( rs.getString( "cgbhbdbh" ) ) );//采购包和标段编号
        jsonData.put( "blockName", Util.null2String( rs.getString( "cgbhbdmc" ) ) );//采购包和标段名称
        jsonData.put( "investmentBudget", Util.null2String( rs.getString( "cgbbtys" ) ) );//采购包或标投预算
        jsonData.put( "note", Util.null2String( rs.getString( "bz1" ) ) );//备注
        jsonData.put( "country", Util.null2String( rs.getString( "mfgbwb" ) ) );//卖方国别
        jsonData.put( "budgetTypes", Util.null2String( rs.getString( "yslxwb" ) ) );//预算类型
        jsonData.put( "evaluationMethod", Util.null2String( rs.getString( "pbpjbf" ) ) );//评标_评价办法
        jsonData.put( "invitationDate", Util.null2String( rs.getString( "fbrq" ) ) );//发标日期
        jsonData.put( "openingDate", Util.null2String( rs.getString( "kbrq" ) ) );//开标日期
        jsonData.put( "performanceGuarantee", Util.null2String( rs.getString( "lydbwb" ) ) );//履约担保
        jsonData.put( "performanceStage", Util.null2String( rs.getString( "lyjdwb" ) ) );//履约阶段
        jsonData.put( "instructions", Util.null2String( rs.getString( "lyjdsm" ) ) );//履约阶段说明
        String sfwhtbg = Util.null2String( rs.getString( "sfwhtbg" ) );
        jsonData.put( "whetherToChange", "0".equals( sfwhtbg ) ? "Y" : "N" );//是否变更
        jsonData.put( "supplierName", Util.null2String( rs.getString( "gysmcwb" ) ) );//卖方名称
        jsonData.put( "supplierCode", Util.null2String( rs.getString( "gysdm" ) ) );//卖方代码
        jsonData.put( "supplierClassify", Util.null2String( rs.getString( "mfxzwb" ) ) );//卖方性质
        jsonData.put( "isQualifiedSupplier", Util.null2String( rs.getString( "gyslxwb" ) ) );//供应商类型
        String sfjtndw = Util.null2String( rs.getString( "sfjtndw" ) );
        jsonData.put( "isInUnitCompany", "0".equals( sfjtndw ) ? "Y" : "N");//是否集团内单位
        jsonData.put( "contractStatus", Util.null2String( rs.getString( "htztwb" )));//合同状态
		jsonData.put( "supplierClassification", Util.null2String( rs.getString( "gysflwb" )));//供应商分类
        jsonData.put( "purchaseContent", Util.null2String( rs.getString( "jtcgnrjs" )));//具体采购内容简述
        jsonData.put( "whetherBidding", Util.null2String( rs.getString( "sfwtzbjgwb" )));//是否委托招标机构
        jsonData.put( "purchaseClass", Util.null2String( rs.getString( "cghtflwb" ))); //采购合同分类
        jsonData.put( "createBy", Util.null2String( rs.getString( "sqrwb" ) ) );//创建人
        jsonData.put( "tenantCode", "ZDKST" );//租户编码
        jsonData.put( "status", "rkz");

        //明细1-收款方对象
        JSONArray payBankArray = new JSONArray();
        rs.executeQuery( "select * from " + tablename + "_dt1 where mainid=?", mainid );
        while (rs.next()) {
            JSONObject payBankJson = new JSONObject();
            payBankJson.put( "payeeName", Util.null2String( rs.getString( "skfdw" ) ) );//收款方名称
            payBankJson.put( "payeeAccount", Util.null2String( rs.getString( "skfzh" ) ) );//收款方账户
            payBankJson.put( "accountBank", Util.null2String( rs.getString( "khx" ) ) );//开户行
            payBankJson.put( "repayAcctNo", Util.null2String( rs.getString( "skfzh" ) ) );//收款方账户
            payBankJson.put( "repayName", Util.null2String( rs.getString( "skfdw" ) ) );//收款方名称
            payBankJson.put( "branch", Util.null2String( rs.getString( "khx" ) ) );//开户行
			payBankJson.put( "fulfillmentPlace", Util.null2String( rs.getString( "sswb" ) ) ); //省市
            payBankJson.put( "unionpay", Util.null2String( rs.getString( "ylh" ) ) ); //银联号
            payBankArray.add( payBankJson );
        }
        jsonData.put( "payBank", payBankArray );

        //明细2-投标人/报价人对象
        JSONArray bidderObjArray = new JSONArray();
        rs.executeQuery( "select tbrbjrmc,tbrbjrdm from " + tablename + "_dt2 where mainid=?", mainid );
        while (rs.next()) {
            JSONObject bidderObjJson = new JSONObject();
            bidderObjJson.put( "bidderName", Util.null2String( rs.getString( "tbrbjrmc" ) ) );//投标人/报价人名称
            bidderObjJson.put( "bidderNumber", Util.null2String( rs.getString( "tbrbjrdm" ) ) );//投标人/报价人代码
            bidderObjArray.add( bidderObjJson );
        }
        jsonData.put( "bidderObj", bidderObjArray );

        //明细3-采购标的
        JSONArray purchObjArray = new JSONArray();
        rs.executeQuery( "select * from " + tablename + "_dt3 where mainid=?", mainid );
        while (rs.next()) {
            JSONObject purchObjJson = new JSONObject();
            purchObjJson.put( "name", Util.null2String( rs.getString( "mc" ) ) );//采购对象名称
            purchObjJson.put( "model", Util.null2String( rs.getString( "xh" ) ) );//采购对象型号           
            purchObjJson.put( "numberOf", Util.null2String( rs.getString( "sl" ) ) );//采购对象数量
            purchObjJson.put( "unitPrice", Util.null2String( rs.getString( "dj" ) ) );//采购对象单价
            purchObjJson.put( "measuringUnit", Util.null2String( rs.getString( "jldw" ) ) );//计量单位
            purchObjJson.put( "manufacturer", Util.null2String( rs.getString( "sccj" ) ) );//生产厂家
            String sfjy = "0".equals( Util.null2String( rs.getString( "sfjy" ) ) ) ? "是" : "否";
            purchObjJson.put( "whetherMilitary", sfjy );//是否军用
            purchObjJson.put( "use", Util.null2String( rs.getString( "ytwb" ) ) );//用途
            purchObjJson.put( "country", Util.null2String( rs.getString( "sccjgb" ) ) );//生产厂家国别
            purchObjArray.add( purchObjJson );
        }
        jsonData.put( "purchObj", purchObjArray );
        String resultText = InvokeHelper.addPaycont( jsonData.toJSONString() );
        baseBean.writeLog( ">>>>addPaycont.jsp>>>>" + resultText );
        JSONObject resultJson = JSONObject.parseObject( resultText );
        String code = Util.null2String( resultJson.getString( "code" ) );
        String data = Util.null2String( resultJson.getString( "data" ) );
        if ("200".equals( code )) {
            rs.executeUpdate( "update uf_xshttz set czzt='1' where id=?", mainid );
            successCount++;
        } else {
            failureCount++;
        }
        SunTaiUtil.saveCallInterLog( "采购合同新增接口", data, mainid, userid );
    }
    returnJson.put( "counts", counts );
    returnJson.put( "successCount", successCount );
    returnJson.put( "failureCount", failureCount );
    out.print( returnJson.toJSONString() );
%>
